package com.education.wisdom.controller.app;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.pojo.*;
import com.education.wisdom.service.IBbsFabulousService;
import com.education.wisdom.service.IBbsFollowService;
import com.education.wisdom.service.IBbsService;
import com.education.wisdom.service.ICatalogBbsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.Collections;

import static com.education.common.core.BaseController.writeJSON;

/**
 * 论坛内容 信息操作处理
 *
 * @author ruoyi
 * @date 2018-11-10
 */
@Controller
@ResponseBody
@CrossOrigin
@Slf4j
@Api(tags = {"职业圈子论坛"})
@RequestMapping("/appBbs")
public class AppBbsController {

    @Autowired
    private IBbsService bbsService;

    @Autowired
    private ICatalogBbsService catalogBbsService;

    @Autowired
    private IBbsFollowService bbsFollowService;


    @Autowired
    private IBbsFabulousService fabulousService;

    @GetMapping("/directoryList")
    public void directoryList(HttpServletResponse response) throws IOException {
        log.info(LogUtil.LogInfo("查询职业圈子论坛目录", new Date()));
        List<CatalogBbs> list = catalogBbsService.selectAll();
        writeJSON(list, response, "200", "成功");
    }

    @PostMapping("/postTopic")
    public void postTopic(HttpServletResponse response, Bbs bbs) throws IOException {
        log.info(LogUtil.LogInfo("发布论坛", new Date()));
        if (bbs.getLtText() != null && bbs.getLtTitle() != null) {
            bbs.setLtCreatetime(new Date());
            bbs.setParentId(0);
            bbs.setLtId(UUIDUtils.getByUUId());
            bbsService.insertBbs(bbs);
            writeJSON("发布成功", response, "200", "成功");
        }
    }

    @GetMapping("/concerned")
    public void concerned(HttpServletResponse response, String userId, Integer pageNo) throws IOException {
        log.info(LogUtil.LogInfo("查询我关注的话题", new Date()));
        int pageSize = 10;
        PageHelper.startPage(pageNo, pageSize);
        List<Bbs> list = bbsFollowService.selectMyDeTopic(userId);
        for (Bbs bbs : list) {
//            SysAppLogin appLogin = loginService.selectSysAppLoginById(bbs.getLtUserid());

//            bbs.setUserName(appLogin.getUserAccount());
//            bbs.setUserIcoUrl(appLogin.getUserIco());

            List<Bbs> list1 = bbsService.selectByParentId(bbs.getId());
            bbs.setPinlunCount(list1.size());

            BbsFabulous bbsFabulous = new BbsFabulous();
            bbsFabulous.setBbsId(bbs.getId());
            List<BbsFabulous> bbsFabulous1 = fabulousService.selectBbsFabulousList(bbsFabulous);
            if (bbsFabulous1 != null && bbsFabulous1.size() > 0) {
                bbs.setDianzanCount(bbsFabulous1.size());
            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @PostMapping("/addAttention")
    public void addAttention(HttpServletResponse response, String userId, Integer bbsId) throws IOException {
        log.info(LogUtil.LogInfo("按钮添加关注", new Date()));
        if (userId != null && bbsId != null) {
            BbsFollow bbsFollow = new BbsFollow();
            bbsFollow.setUserId(userId);
            bbsFollow.setBbsId(bbsId);
            List<BbsFollow> bbsFollows = bbsFollowService.selectBbsFollowList(bbsFollow);
            Map<String, String> map = new HashMap<>();
            if (bbsFollows != null && bbsFollows.size() > 0) {
                bbsFollowService.deleteBbsFollowByIds(bbsFollows.get(0).getId().toString());
                map.put("status", "0");
                List<BbsFollow> bbsFollows1 = bbsFollowService.selectByBbsId(bbsId);
                map.put("count", bbsFollows1.size() + "");
            } else {
                bbsFollow.setCreateTime(new Date());
                bbsFollowService.insertBbsFollow(bbsFollow);
                map.put("status", "1");
                List<BbsFollow> bbsFollows1 = bbsFollowService.selectByBbsId(bbsId);
                map.put("count", bbsFollows1.size() + "");
            }
            writeJSON(map, response, "200", "成功");
        }
        writeJSON("未登录，请登录", response, "201", "成功");
    }

    @GetMapping("/initiated")
    public void initiated(HttpServletResponse response, String userId, Integer pageNo) throws IOException {
        log.info(LogUtil.LogInfo("查询我发起的话题", new Date()));
        int pageSize = 10;
        PageHelper.startPage(pageNo, pageSize);
        List<Bbs> list = bbsService.selectByUserId(userId);
        for (Bbs bbs : list) {
            CatalogBbs catalogBbs = catalogBbsService.selectByCatalogId(bbs.getCatalogId());
            bbs.setCatalogName(catalogBbs.getName());
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @GetMapping("/topicDetails")
    public void topicDetails(HttpServletResponse response, Integer id, String userId) throws IOException {
        log.info(LogUtil.LogInfo("查询话题详情", new Date()));
        Bbs bbs = bbsService.selectBbsById(id);
        if (bbs.getViewsCount() == null) {
            bbs.setViewsCount(1);
        } else {
            bbs.setViewsCount(bbs.getViewsCount() + 1);
        }

        bbsService.updateBbs(bbs);

//        SysAppLogin appLogin = loginService.selectSysAppLoginById(bbs.getLtUserid());
//        if (appLogin != null) {
//            bbs.setUserName(appLogin.getUserAccount());
//            bbs.setUserIcoUrl(appLogin.getUserIco());
//            BbsFabulous fabulous = new BbsFabulous();
//            fabulous.setUserId(userId);
//            fabulous.setBbsId(bbs.getId());
//
//            List<BbsFabulous> bbsFabulous = fabulousService.selectBbsFabulousList(fabulous);
//            if (bbsFabulous != null && bbsFabulous.size() > 0) {
//                bbs.setDoYouLikeIt("1");
//            } else {
//                bbs.setDoYouLikeIt("0");
//            }
//
//            BbsFollow bbsFollow = new BbsFollow();
//            bbsFollow.setBbsId(bbs.getId());
//            bbsFollow.setUserId(userId);
//
//            List<BbsFollow> bbsFollows = bbsFollowService.selectBbsFollowList(bbsFollow);
//            if (bbsFollows != null && bbsFollows.size() > 0) {
//                bbs.setShifouguanzhu("1");
//            } else {
//                bbs.setShifouguanzhu("0");
//            }
//
//            List<Bbs> list1 = bbsService.selectByParentId(bbs.getId());
//            bbs.setPinlunCount(list1.size());
//
//            BbsFabulous bbsFabulous2 = new BbsFabulous();
//            bbsFabulous2.setBbsId(bbs.getId());
//            List<BbsFabulous> bbsFabulous1 = fabulousService.selectBbsFabulousList(bbsFabulous2);
//            if (bbsFabulous1 != null && bbsFabulous1.size() > 0) {
//                bbs.setDianzanCount(bbsFabulous1.size());
//            }
//        }
        writeJSON(bbs, response, "200", "成功");
    }

    @GetMapping("/detailsList")
    public void detailsList(HttpServletResponse response, Integer BbsId, Integer pageNo) throws IOException {
        log.info(LogUtil.LogInfo("查询底下详情列表", new Date()));
        int pageSize = 10;
        PageHelper.startPage(pageNo, pageSize);
        List<Bbs> list = bbsService.selectByParentId(BbsId);
        for (Bbs bbs : list) {
//            SysAppLogin appLogin = loginService.selectSysAppLoginById(bbs.getLtUserid());
//            if (appLogin != null) {
//                bbs.setUserName(appLogin.getUserAccount());
//                bbs.setUserIcoUrl(appLogin.getUserIco());
//                BbsFabulous fabulous = new BbsFabulous();
//                fabulous.setUserId(bbs.getLtUserid());
//                fabulous.setBbsId(bbs.getId());
//
//                List<BbsFabulous> bbsFabulous = fabulousService.selectBbsFabulousList(fabulous);
//                if (bbsFabulous != null && bbsFabulous.size() > 0) {
//                    bbs.setDoYouLikeIt("1");
//                } else {
//                    bbs.setDoYouLikeIt("0");
//                }
//            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @PostMapping("/topicReviewAdded")
    public void topicReviewAdded(HttpServletResponse response, Bbs bbs) throws IOException {
        log.info(LogUtil.LogInfo("添加话题详情", new Date()));
        if (bbs.getLtText() != null) {
            bbs.setLtCreatetime(new Date());
            String byUUId = UUIDUtils.getByUUId();
            bbs.setLtId(byUUId);
            bbsService.insertBbs(bbs);
            writeJSON(bbsService.selectByUuId(byUUId), response, "200", "成功");
        }
    }

    @GetMapping("/list")
    public void list(HttpServletResponse response, Integer pageNo, Integer catalogId, String userId) throws IOException {
        log.info(LogUtil.LogInfo("查询论坛列表", new Date()));
        int pageSize = 10;
        PageHelper.startPage(pageNo, pageSize);
        List<Bbs> list = bbsService.selectByCatalogId(catalogId);
        for (Bbs bbs : list) {
//            SysAppLogin appLogin = loginService.selectSysAppLoginById(bbs.getLtUserid());
//            bbs.setUserName(appLogin.getUserAccount());
//            bbs.setUserIcoUrl(appLogin.getUserIco());
//            List<Bbs> list1 = bbsService.selectByParentId(bbs.getId());
//            bbs.setPinlunCount(list1.size());
//            BbsFollow bbsFollow = new BbsFollow();
//            bbsFollow.setBbsId(bbs.getId());
//            bbsFollow.setUserId(userId);

//            List<BbsFollow> bbsFollows = bbsFollowService.selectBbsFollowList(bbsFollow);
//            if (bbsFollows != null && bbsFollows.size() > 0) {
//                bbs.setShifouguanzhu("1");
//            } else {
//                bbs.setShifouguanzhu("0");
//            }
//
//            BbsFabulous bbsFabulous = new BbsFabulous();
//            bbsFabulous.setBbsId(bbs.getId());
//            List<BbsFabulous> bbsFabulous1 = fabulousService.selectBbsFabulousList(bbsFabulous);
//            if (bbsFabulous1 != null && bbsFabulous1.size() > 0) {
//                bbs.setDianzanCount(bbsFabulous1.size());
//            }
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @GetMapping("/featuredTopics")
    public void featuredTopics(HttpServletResponse response, Integer pageNo, String userId) throws IOException {
        log.info(LogUtil.LogInfo("社区精选话题，我关注的话题", new Date()));
        int pageSize = 10;
        PageHelper.startPage(pageNo, pageSize);
        List<Bbs> bbs = bbsFollowService.selectMyDeTopic(userId);
        writeJSON(new PageInfo<>(bbs), response, "200", "成功");
    }

    @GetMapping("/randomTopic")
    public void randomTopic(HttpServletResponse response, Integer count) throws IOException {
        log.info(LogUtil.LogInfo("推荐话题，一周话题榜", new Date()));
        List<Bbs> list = bbsService.selectByParentId(0);
        Collections.shuffle(list);
        if (count > list.size()) {
            writeJSON(list, response, "200", "成功");
        } else {
            list = list.subList(0, count);
            writeJSON(list, response, "200", "成功");
        }
    }

    @PostMapping("/addLike")
    public void addLike(HttpServletResponse response, String userId, Integer bbsId) throws IOException {
        log.info(LogUtil.LogInfo("点赞", new Date()));
        BbsFabulous fabulous = new BbsFabulous();
        fabulous.setUserId(userId);
        fabulous.setBbsId(bbsId);
        if (fabulous.getUserId() != null) {
            List<BbsFabulous> bbsFabulous = fabulousService.selectBbsFabulousList(fabulous);
            Map<String, String> map = new HashMap<>();
            if (bbsFabulous != null && bbsFabulous.size() > 0) {
                fabulousService.deleteBbsFabulousByIds(bbsFabulous.get(0).getId().toString());
                map.put("status", "0");
                List<BbsFabulous> list = fabulousService.selectByBbsId(bbsId);
                map.put("count", list.size() + "");
            } else {
                fabulous.setCreateTime(new Date());
                fabulousService.insertBbsFabulous(fabulous);
                map.put("status", "1");
                List<BbsFabulous> list = fabulousService.selectByBbsId(bbsId);
                map.put("count", list.size() + "");
            }
            writeJSON(map, response, "200", "成功");
        } else {
            writeJSON("未登录，请登录", response, "201", "成功");
        }
    }
}
